package xx.iverson.school_manage.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import xx.iverson.school_manage.entity.Points;
import xx.iverson.school_manage.service.PointsService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wei77
 */
@Controller
public class ToHeadController {

    @Resource
    private PointsService pointsService;

    // 跳转到负责人页面
    @RequestMapping("toHeadIndex")
    public String toHeadIndex(@RequestParam(value = "letter", defaultValue = "") String letter, @RequestParam(value = "name", defaultValue = "") String name, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum, Model model) {
        if (name != null && !name.isEmpty()) {
            PageHelper.startPage(pageNum, 100);
            List<Points> points = pointsService.getLikePoints(name);
            PageInfo<Points> pageInfo = new PageInfo<>(points);
            model.addAttribute("points", pageInfo);
            model.addAttribute("pageNum", pageInfo.getPageNum());
            model.addAttribute("pages", pageInfo.getPages());
        } else if (letter != null && !letter.isEmpty()) {
            PageHelper.startPage(pageNum, 100);
            List<Points> points = pointsService.getLetterPoints(letter);
            PageInfo<Points> pageInfo = new PageInfo<>(points);
            model.addAttribute("points", pageInfo);
            model.addAttribute("pageNum", pageInfo.getPageNum());
            model.addAttribute("pages", pageInfo.getPages());
        } else {
            // 开始分页
            PageHelper.startPage(pageNum, 10);
            List<Points> points = pointsService.getListPoints(0);
            PageInfo<Points> pageInfo = new PageInfo<>(points);
            model.addAttribute("points", pageInfo);
            model.addAttribute("pageNum", pageInfo.getPageNum());
            model.addAttribute("pages", pageInfo.getPages());
        }
        return "head/headIndex";
    }

    // 去负责人修改密码页面
    @RequestMapping("toHeadUpdate")
    public String toHeadUpdate() {
        return "head/me";
    }
}
